package com.lzy.business.controller.sys;

import com.github.pagehelper.PageInfo;
import com.lzy.base.apis.SysRoleResourceServiceApi;
import com.lzy.base.apis.SysRoleServiceApi;
import com.lzy.base.constants.Constants;
import com.lzy.base.dtos.SysRoleDto;
import com.lzy.business.request.RoleListRequest;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 角色
 *
 * @author lc
 * @date 2017年1月1日 下午6:20:38
 */
@Controller
@RequestMapping("admin/role")
public class SysRoleController {
    @Autowired
    private SysRoleServiceApi sysRoleServiceApi;
    @Autowired
    private SysRoleResourceServiceApi sysRoleResourceServiceApi;

    /**
     * 默认页面
     **/
    @RequestMapping(method = RequestMethod.GET)
    public String list() {
        return "admin/roleList";
    }

    /**
     * 角色集合 JSON
     **/
//    @RequiresPermissions("sys:role:view")
    @RequestMapping(value = "json", method = RequestMethod.GET)
    @ResponseBody
    public PageBean getData(RoleListRequest request) {
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());

        SysRoleDto roleDto = DozerUtils.map(request, SysRoleDto.class);
        PageInfo<SysRoleDto> pageInfo = sysRoleServiceApi.selectRole(pager, roleDto);
        Ensure.that(pageInfo).isNotNull("01001126");
        if (pageInfo != null && pageInfo.getList() != null) {
            return new PageBean(pageInfo.getTotal(), pageInfo.getList());
        } else {
            return new PageBean();
        }
    }

    /**
     * 获取角色拥有的权限ID集合
     *
     * @param roleId
     * @return
     **/
//    @RequiresPermissions("sys:role:resView")
    @RequestMapping(value = "res/json", method = RequestMethod.GET)
    @ResponseBody
    public List<Long> getRoleResources(@RequestParam("roleId") Long roleId) {
        List<Long> list = sysRoleResourceServiceApi.getResourceIds(roleId);
        return list;
    }

    /**
     * 修改角色权限
     *
     * @param roleId
     * @param newResourceIds
     * @return
     **/
//    @RequiresPermissions("sys:role:resUpdate")
    @RequestMapping(value = "{id}/updateRoleRes", method = RequestMethod.POST)
    @ResponseBody
    public Result updateRolePermission(@PathVariable("id") Long roleId,
                                       @RequestBody List<Long> newResourceIds) {
        SysRoleDto sysRole = sysRoleServiceApi.selectById(roleId);
        Ensure.that(sysRole.getStatus() == Constants.SYS_PERMISSION_ADMIN).isFalse("01001122");
        List<Long> oldResourceIds = sysRoleResourceServiceApi
                .getResourceIds(roleId);
        Ensure.that(sysRoleResourceServiceApi.updateRoleResource(roleId, oldResourceIds,
                newResourceIds)).isTrue("01001127");
        return new Result();

    }

    /**
     * 添加角色跳转
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(Model model) {
        model.addAttribute("role", new SysRoleDto());
        model.addAttribute("action", "create");
        return "admin/roleForm";
    }

    /**
     * 添加角色
     *
     * @param role
     * @param bindingResult
     * @return
     */
//    @RequiresPermissions("sys:role:add")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public Result create(@Valid SysRoleDto role, BindingResult bindingResult) {
        Ensure.that(bindingResult.hasErrors()).isFalse("01001124");
        Ensure.that(role).isNotNull("00000004");
        Ensure.that(role.getRlname()).isNotNull("00000015");
        Ensure.that("".equals(role.getRlname())).isFalse("00000015");
        Ensure.that(sysRoleServiceApi.insertSelective(role)).isTrue("01001123");
        return new Result();
    }

    /**
     * 修改角色跳转
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.GET)
    public String updateForm(@RequestParam("id") Long id, Model model) {
        model.addAttribute("role", sysRoleServiceApi.selectById(id));
        model.addAttribute("action", "update");
        return "admin/roleForm";
    }

    /**
     * 修改角色
     *
     * @param sysRole
     * @param bindingResult
     * @return
     */
//    @RequiresPermissions("sys:role:update")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Result updateRole(@Valid @ModelAttribute("role") SysRoleDto sysRole, BindingResult bindingResult) {
        SysRoleDto sysRoleAll = sysRoleServiceApi.selectById(sysRole.getId());
        Ensure.that(bindingResult.hasErrors()).isFalse("05000005");
        Ensure.that(sysRoleAll.getStatus().intValue() == Constants.SYS_PERMISSION_ADMIN).isFalse("05000003");
        Ensure.that(sysRoleServiceApi.update(sysRole)).isTrue("05000004");
        return new Result();
    }

    /**
     * 删除角色
     *
     * @return
     */
//    @RequiresPermissions("sys:role:delete")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteRole(SysRoleDto sysRole) {
        //系统初始化角色不可更改
        sysRoleServiceApi.delete(sysRole.getId());
        return new Result();
    }

}
